<?php
namespace app\common\logic\home;

use app\common\model\UserModel;
use app\common\model\AppUserModel;
use app\common\model\RoleUserModel;

class Account
{

	//判断注册进度
	////1 成功 2完善信息 
	//-1 请选择身份 -2注册申请中 -3注册被拒绝
	public function type($data){

		if(empty($data['role'])){
			return [
					'code'=>-1,
					'msg'=>'请选择身份',
				];
		}
		//如果是教师角色
		if($data['role'] == 2){
			switch ($data['status']) {
				case '0':
					return [
						'code'=>-2,
						'msg'=>'注册申请中',
					];
					break;
				case '2':
					return [
						'code'=>-3,
						'msg'=>'注册被拒绝',
					];
					break;
			}

		}elseif($data['role'] == 1){
			if(empty($data['realName']) || empty($data['schoolName']) || empty($data['grad']) || empty($data['classes']) || empty($data['entranceTime'])){
				return [
							'code'=>2,
							'role'=>$data['role'],
							'id'=>$data['id'],
							'msg'=>'请学生完善信息',
						];
						break;
			}
		}
		return  [
					'role'=>$data['role'],
					'code'=>1,
					'id'=>$data['id'],
					'msg'=>'登录成功',
				];
	}




	/**
	 * [添加账号]
	 * @param  [type] $param [description]
	 */
	public function adds($param){
		$data['phone'] = $param['phone'];
		$data['createTime'] = date('Y-m-d H:i:s');
        $data['password']   = sha1($param['password']);
        $AppUserModel = new AppUserModel();
        $rs = $AppUserModel->save($data);
        return $AppUserModel->id;
	}
	/**
	 * [选择身份]
	 * @param [type] $param.uid [用户uid]
	 * @param [type] $param.user_type [1 admin-老师 2用户-学生]
	 * 0 没有用户信息 1选择完成 -1没有备案
	 */
	public function selectTypes($param){	
		$data = AppUserModel::get($param['uid']);
		if(empty($data)){
			return [
					'code'=>0,
					'msg'=>'没有用户信息'
				];
		}

		if($param['user_type']==2){
			$data_app_user['role'] = 1;//学生
			$rs = AppUserModel::where('id',$param['uid'])->update($data_app_user);
			return [
						'code'=>1,
						'uid' => $data['id'],
						'role' => $data_app_user['role'],
				];
		}else{
			if($data['role'] != 2){
				return [
					'code'=>-1,
					'msg'=>'您的信息没有备案',
				];
			}
			return [
						'code'=>1,
						'uid' => $data['id'],
						'role' => $data['role'],
				];
		}


	}
	/**
	 * [微信绑定手机号]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.phone [用户手机号]
	 * @param [type] $param.code [用户验证码]
	 */
	public function bidding($param){
		$rs = cmf_check_verification_code($param['phone'],$param['code']);
        if($rs){
        	return [
        			'code'=>0,
        			'msg'=>'验证码错误',
        		];
        }
       	$data_app_user = AppUserModel::get(['phone'=>$param['phone']]);
       	//如果已经注册
       	if($data_app_user){
       		//更新绑定关系
       		UserModel::where('app_id',$data_app_user['id'])->update(['app_id'=>'']);
       		$rs = UserModel::where('unionid',$param['unionid'])->update(['app_id'=>$data_app_user['id'],'user_login'=>$data_app_user['phone']]);
       		if($rs){
       			return [
       				'code'=>1,
       				'uid'=>$data_app_user['id'],
       				'msg'=>'绑定成功',
       			];
       		}else{
       			return [
       				'code'=>0,
        			'msg'=>'绑定失败',
       			];
       		}
       	//如果没有注册
       	}else{
       		return [
       				'code'=>-1000,
        			'msg'=>'请设置密码',
       			];
       	}
	}
	/**
	 * [添加账号,并绑定微信]
	 * @param  [type] $param [description]
	 */
	public function addsWx($param){
		//新增
   		$data_user = UserModel::get(['unionid'=>$param['unionid']]);
   		//微信信息
   		$more = json_decode($data_user['more'],true);
   		if($more['sex'] == 1){
   			$sex = '男';
   		}elseif($more['sex'] == 2){
   			$sex = '女';
   		}else{
   			$sex = NULL;
   		}
   		//appuser表信息
   		$data = [
   				'phone' => $param['phone'],
   				'header' => $more['headimgurl'],
   				//'realName' => $more['nickname'],
   				'nickname' => $more['nickname'],
   				'createTime' => date('Y-m-d H:i:s'),
    			'password'  => sha1($param['password']),
   				'sex' => $sex,
   			];
   		//保存user_app表内容
   		$AppUser = new AppUserModel();
   		$rs = $AppUser->save($data);
   		//绑定user_app表和user表一对一关系
   		$rs = UserModel::where('unionid',$param['unionid'])->update(['app_id'=>$AppUser->id]);
   		if($rs){
   			return [
   				'code'=>1,
   				'msg'=>'绑定成功',
   				'uid'=>$AppUser->id,
   			];
   		}else{
   			return [
   				'code'=>0,
    			'msg'=>'绑定失败',
   			];
   		}
		
	}


	/**
	 * [未授权时添加]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.openid [用户openid]
	 * @param [type] $param.nickname [用户昵称]
	 * @param [type] $param.headimgurl [用户头像]
	 * @param [type] $param.sex [性别]
	 * @param [type] $param.more [全部获得的信息]
	 */
	public function add($param){
		$data_user = [
				'avatar' => $param['headimgurl'],
				'user_nickname' => $param['nickname'],
				'unionid' => $param['unionid'],
				'openid' => $param['openid'],
				'sex'=>$param['sex'],
				'more' => htmlspecialchars_decode($param['more'])
			];
		$UserModel = new UserModel();
		$rs = $UserModel->save($data_user);
		if ($rs) {
			return $UserModel->id;
		}else{
			return 0;
		}
	}

	

























	/**
	 * [微信登录]
	 * @return [type]        [description]
	 */
	public function login已废弃($param){
		$data = UserModel::get(['unionid'=>$param['unionid']]);
		//如果为空添加
		if(empty($data)){
			$rs = $this->add($param);
			if($rs){
				return [
						'code'=>1,
						'msg'=>'请绑定手机号',
					];
			}else{
				return [
						'code' => 0,
						'msg' => '登录失败',
					];
			}
		}else{
			if(empty($data['app_id'])){
				return [
						'code'=>1,
						'msg'=>'请绑定手机号',
					];
			}
			if(empty($data['user_type'])){
				return [
						'code'=>2,
						'msg'=>'请选择身份',
					];
			}
			//如果是教师角色
			if($data['appUser']['role'] == 2){
				switch ($data['appUser']['status']) {
					case '0':
						return [
							'code'=>6,
							'msg'=>'注册申请中',
						];
						break;
					case '2':
						return [
							'code'=>4,
							'msg'=>'注册被拒绝',
						];
						break;
				}

				/**if(empty($data['appUser']['realName'])){
					return [
								'code'=>7,
								'id'=>$data['app_id'],
								'msg'=>'请完善老师信息',
							];
							break;
				}*/
			}elseif($data['appUser']['role'] == 1){
				if(empty($data['appUser']['realName']) || empty($data['appUser']['schoolName']) || empty($data['appUser']['grad']) || empty($data['appUser']['classes']) || empty($data['appUser']['entranceTime'])){
					return [
								'code'=>8,
								'role'=>$data['appUser']['role'],
								'id'=>$data['app_id'],
								'msg'=>'请学生完善信息',
							];
							break;
				}
			}
			//获取token
			return [
					'role'=>$data['appUser']['role'],
					'code'=>5,
					'id'=>$data['app_id'],
					'msg'=>'登录成功',
				];

		}
	}

	


	/**
	 * [绑定手机号]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.phone [用户手机号]
	 * @param [type] $param.code [用户验证码]
	 */
	public function bidding111已废弃($param){
		$rs = cmf_check_verification_code($param['phone'],$param['code']);
        if($rs){
        	return [
        			'code'=>0,
        			'msg'=>'验证码错误',
        		];
        }
       	$data_app_user = AppUserModel::get(['phone'=>$param['phone']]);
       	//如果已经注册
       	if($data_app_user){
       		//更新绑定关系
       		UserModel::where('app_id',$data_app_user['id'])->update(['app_id'=>'']);
       		$rs = UserModel::where('unionid',$param['unionid'])->update(['app_id'=>$data_app_user['id'],'user_login'=>$data_app_user['phone']]);
       		if($rs){
       			return [
       				'code'=>1,
       				'uid'=>$data_app_user['id'],
       				'msg'=>'绑定成功',
       			];
       		}else{
       			return [
       				'code'=>0,
        			'msg'=>'绑定失败',
       			];
       		}
       	//如果没有注册
       	}else{
       		//新增
       		$data_user = UserModel::get(['unionid'=>$param['unionid']]);
       		//微信信息
       		$more = json_decode($data_user['more'],true);
       		if($more['sex'] == 1){
       			$sex = '男';
       		}elseif($more['sex'] == 2){
       			$sex = '女';
       		}else{
       			$sex = NULL;
       		}
       		//appuser表信息
       		$user = [
       				'phone' => $param['phone'],
       				'header' => $more['headimgurl'],
       				//'realName' => $more['nickname'],
       				'nickname' => $more['nickname'],
       				'sex' => $sex,
       			];
       		//保存user_app表内容
       		$AppUser = new AppUserModel();
       		$rs = $AppUser->save($user);
       		//绑定user_app表和user表一对一关系
       		$rs = UserModel::where('unionid',$param['unionid'])->update(['app_id'=>$AppUser->id]);
       		if($rs){
       			return [
       				'code'=>1,
       				'msg'=>'绑定成功',
       			];
       		}else{
       			return [
       				'code'=>0,
        			'msg'=>'绑定失败',
       			];
       		}
       	}
	}

	/**
	 * [选择身份]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.user_type [1 admin-老师 2用户-学生]
	 */
	public function selectType已废弃($param){
		$data = UserModel::get(['unionid'=>$param['unionid']]);
		$app_id = $data['app_id'];//appid
		$user_id = $data['id'];//用户ID
		if($param['user_type'] == 1){
			$data_user['user_type'] = 1;//admin
			$data_app_user['role'] = 2;//老师
			//除了通过和申请之外,更新账号状态
			if($data['appUser']['status'] == 2 || $data['appUser']['status'] == 1){
				$data_app_user['status'] = 0;//申请中
			}
			$data_role_user = [
					'user_id'=>$user_id,
					'role_id'=>3,
				];
			$RoleUser = new RoleUserModel();
			$RoleUser->save($data_role_user);
		}else{
			$data_user['user_type'] = 2;//用户
			$data_app_user['role'] = 1;//学生
			RoleUserModel::where('user_id',$user_id)->delete();
		}
		$data_user['update_time'] = time();
		$rs = AppUserModel::where('id',$app_id)->update($data_app_user);
		$rs = UserModel::where('unionid',$param['unionid'])->update($data_user);

		if($rs){
			return [
					'code'=>1,
					'app_id' => $app_id,
					'role' => $data_app_user['role'],
				];
		}else{
			return [
					'code'=>0,
				];
		}
		
	}
}
